package com.guan.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guan.eduservice.entity.EduChapter;
import com.guan.eduservice.entity.EduCourse;
import com.guan.eduservice.entity.EduCourseDescription;
import com.guan.eduservice.entity.frontVo.CourseFrontVo;
import com.guan.eduservice.entity.frontVo.CourseWebVo;
import com.guan.eduservice.entity.vo.CourseInfoVo;
import com.guan.eduservice.entity.vo.CoursePublishVo;
import com.guan.eduservice.mapper.EduCourseMapper;
import com.guan.eduservice.service.EduChapterService;
import com.guan.eduservice.service.EduCourseDescriptionService;
import com.guan.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guan.eduservice.service.EduVideoService;
import com.guan.servicebase.exceptonhandler.GuliException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-01-01
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;


    /**
     * 添加课程信息
     * @param courseInfoVo
     */
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //向课程表添加数据
        EduCourse eduCourse = new EduCourse(); //新建EduCourse对象
        BeanUtils.copyProperties(courseInfoVo,eduCourse); //将courseInfoVo属性值复制到eduCourse
        int insert = baseMapper.insert(eduCourse); //传参 返回添加成功的条数
        if (insert<=0){
            throw new GuliException(20001,"添加课程信息失败！");
        }
        //获取添加成功的课程id
        String cid = eduCourse.getId();
        //向课程描述表添加数据
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置描述id 才会建立一对一的关系
        courseDescription.setId(cid);
        boolean save = courseDescriptionService.save(courseDescription);
        if (save==false){
            throw new GuliException(20001,"添加课程信息失败！");
        }
        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo vo = new CourseInfoVo();
        //1. 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse,vo);
        //2. 查询课程描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        vo.setDescription(courseDescription.getDescription());
        //3. 返回
        return vo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        //先修改课程表
        int update = baseMapper.updateById(eduCourse);
        if (update<=0){
            throw new GuliException(20001,"修改课程信息失败！");
        }

        //修改描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        boolean update2 = courseDescriptionService.updateById(eduCourseDescription);
        if (update2==false){
            throw new GuliException(20001,"修改课程信息失败！");
        }

    }

    /**
     * 根据课程id查询课程确认信息
     * @param courseId
     * @return
     */
    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(courseId);
        return publishCourseInfo;
    }

    /**
     * 删除课程
     * 删除顺序：小节、章节、描述、课程
     * @param courseId
     * @return
     */
    @Override
    public void removeCourse(String courseId) {
        //删小节
        eduVideoService.removeVideoByCoureId(courseId);
        //删章节
        eduChapterService.removeChapterByCourseId(courseId);
        //删描述
        courseDescriptionService.removeById(courseId);
        //删课程本身
        int result = baseMapper.deleteById(courseId);
        if (result==0){
            throw new GuliException(20001,"删除失败");
        }
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> coursePage, CourseFrontVo courseFrontVo) {
        //根据讲师id查询所讲课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(courseFrontVo.getSubjectParentId()),"subject_parent_id",courseFrontVo.getSubjectParentId());
        wrapper.eq(StringUtils.isNotEmpty(courseFrontVo.getSubjectId()),"subject_id",courseFrontVo.getSubjectId());
        wrapper.orderByDesc(StringUtils.isNotEmpty(courseFrontVo.getBuyCountSort()),"buy_count",courseFrontVo.getBuyCountSort());
        wrapper.orderByDesc(StringUtils.isNotEmpty(courseFrontVo.getGmtCreateSort()),"gmt_create",courseFrontVo.getGmtCreateSort());
        wrapper.orderByDesc(StringUtils.isNotEmpty(courseFrontVo.getPriceSort()),"price",courseFrontVo.getPriceSort());
        baseMapper.selectPage(coursePage,wrapper);

        List<EduCourse> records = coursePage.getRecords();
        long current = coursePage.getCurrent();
        long pages = coursePage.getPages();
        long size = coursePage.getSize();
        long total = coursePage.getTotal();
        boolean hasNext = coursePage.hasNext();
        boolean hasPrevious = coursePage.hasPrevious();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String couseId) {

        return baseMapper.getBaseCourseInfo(couseId);
    }
}
